import cn.hutool.json.JSONUtil;
import com.heima.article.ArticleApplication;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

@SpringBootTest(classes = ArticleApplication.class)
public class testCreateHtml {
    @Resource
    private Configuration configuration;
    @Resource
    private ApArticleContentMapper contentMapper;
    @Resource
    private FileStorageService fileStorageService;
    @Resource
    private ApArticleMapper apArticleMapper;

    @Test
    public void testCreateHtml1() throws IOException, TemplateException {
        //获取模板对象
        Template template = configuration.getTemplate("article.ftl");
        //通过文章id获取一个文章内容json，然后把字符串转换为对象
        ApArticleContent articleContent = contentMapper.selectById(1302862388957036545l);
        String content = articleContent.getContent();
        HashMap<String, Object> data = new HashMap<>();
        data.put("content", JSONUtil.parseArray(content));
        //把模板和数据交给freemarker生成静态的页面
        template.process(data, new FileWriter("e://" + articleContent.getArticleId() + ".html"));
        //上传文件得到访问地址
        FileInputStream in = new FileInputStream("e://" + articleContent.getArticleId() + ".html");
        String path = fileStorageService.uploadHtmlFile("", articleContent.getArticleId() + ".html", in);
        ApArticle apArticle = new ApArticle();
        apArticle.setId(articleContent.getArticleId());
        apArticle.setStaticUrl(path);
        apArticleMapper.updateById(apArticle);


    }
}
